package com.trhui.mallbook.client;

import com.trhui.mallbook.common.exception.SdkClientException;
import com.trhui.mallbook.domain.common.ApiRequest;
import com.trhui.mallbook.domain.common.BaseResponse;
import com.trhui.mallbook.domain.request.QueryOrderRequest;
import com.trhui.mallbook.domain.response.trade.*;
import com.trhui.mallbook.domain.response.xib.XibSplitResponse;
import com.trhui.mallbook.enums.ChannelTypeEnum;
import com.trhui.mallbook.enums.ServerCodeEnum;

/**
 * @author huangh
 * @version 1.0
 * @description 交易查询接口
 * @date 2023/08/16 0016 15:36
 */

public class TradeQueryClient extends AbstractClient {

    /**
     * 汇付 - 交易查询接口
     * @param request
     * @return
     */
    public static BaseResponse hfTradeQuery(QueryOrderRequest request) {
        BaseResponse baseResponse;
        switch (request.getQueryType()) {
            case "1":
                baseResponse = hfPaymentQuery(request);
                return baseResponse;
            case "2":
                baseResponse = hfRefundQuery(request);
                return baseResponse;
            case "3":
                baseResponse = hfReceiveQuery(request);
                return baseResponse;
            case "4":
                baseResponse = hfWithdrawQuery(request);
                return baseResponse;
            case "5":
                baseResponse = hfDepositQuery(request);
                return baseResponse;
            case "6":
                baseResponse = hfTransferQuery(request);
                return baseResponse;
            case "7":
                baseResponse = hfCompleteQuery(request);
                return baseResponse;
            case "8":
                baseResponse = hfBackSplitQuery(request);
                return baseResponse;
            default:
                throw new SdkClientException("查询类型错误！");
        }
    }

    /**
     * 厦门国际银行- 交易查询接口
     * @param request
     * @return
     */
    public static BaseResponse xibTradeQuery(QueryOrderRequest request) {
        BaseResponse baseResponse;
        switch (request.getQueryType()) {
            case "1":
                baseResponse = xibPaymentQuery(request);
                return baseResponse;
            case "2":
                baseResponse = xibRefundQuery(request);
                return baseResponse;
            case "3":
                baseResponse = xibReceiveQuery(request);
                return baseResponse;
            case "4":
                baseResponse = xibWithdrawQuery(request);
                return baseResponse;
            case "5":
                baseResponse = xibDepositQuery(request);
                return baseResponse;
            case "6":
                baseResponse = xibTransferQuery(request);
                return baseResponse;
            case "7":
                baseResponse = xibCompleteQuery(request);
                return baseResponse;
            case "8":
                baseResponse = xibBackSplitQuery(request);
                return baseResponse;
            case "9":
                baseResponse = xibSplitQuery(request);
                return baseResponse;
            default:
                throw new SdkClientException("查询类型错误！");
        }
    }

    /**
     * 易宝 - 交易查询接口
     * @param request
     * @return
     */
    public static BaseResponse ybTradeQuery(QueryOrderRequest request) {
        BaseResponse baseResponse;
        switch (request.getQueryType()) {
            case "1":
                baseResponse = ybPaymentQuery(request);
                return baseResponse;
            case "2":
                baseResponse = ybRefundQuery(request);
                return baseResponse;
            case "3":
                baseResponse = ybReceiveQuery(request);
                return baseResponse;
            case "4":
                baseResponse = ybWithdrawQuery(request);
                return baseResponse;
            case "5":
                baseResponse = ybDepositQuery(request);
                return baseResponse;
            case "6":
                baseResponse = ybTransferQuery(request);
                return baseResponse;
            case "7":
                baseResponse = ybCompleteQuery(request);
                return baseResponse;
            case "8":
                baseResponse = ybBackSplitQuery(request);
                return baseResponse;
            default:
                throw new SdkClientException("查询类型错误！");
        }
    }



    /**
     * 斗拱 - 交易查询接口
     * @param request
     * @return
     */
    public static BaseResponse dgTradeQuery(QueryOrderRequest request) {
        BaseResponse baseResponse;
        switch (request.getQueryType()) {
            case "1":
                baseResponse = dgPaymentQuery(request);
                return baseResponse;
            case "2":
                baseResponse = dgRefundQuery(request);
                return baseResponse;
            case "4":
                baseResponse = dgWithdrawQuery(request);
                return baseResponse;
            case "7":
                baseResponse = dgCompleteQuery(request);
                return baseResponse;
            case "8":
                baseResponse = dgBackSplitQuery(request);
                return baseResponse;
            default:
                throw new SdkClientException("查询类型错误！");
        }
    }

    /**
     * 斗拱-异步分账查询
     * @param request
     * @return
     */
    private static BaseResponse dgCompleteQuery(QueryOrderRequest request) {
        ApiRequest apiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.DG)
                .build();
        return post(apiRequest, CompleteQueryResponse.class);

    }

    /**
     * 斗拱-结算查询
     * @param request
     * @return
     */
    private static BaseResponse dgWithdrawQuery(QueryOrderRequest request) {
        ApiRequest apiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.DG)
                .build();
        return post(apiRequest, WithdrawQueryResponse.class);
    }

    /**
     * 斗拱-退款查询
     * @param request
     * @return
     */
    private static BaseResponse dgRefundQuery(QueryOrderRequest request) {
        ApiRequest apiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.DG)
                .build();
        return post(apiRequest, RefundQueryResponse.class);
    }

    /**
     * 斗拱-支付查询
     * @param request
     * @return
     */
    private static BaseResponse dgPaymentQuery(QueryOrderRequest request) {
        ApiRequest apiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.DG)
                .build();
        return post(apiRequest, PaymentQueryResponse.class);
    }

    /**
     * 斗拱-分账归还查询
     * @param request
     * @return
     */
    private static BaseResponse dgBackSplitQuery(QueryOrderRequest request) {
        ApiRequest apiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.DG)
                .build();
        return post(apiRequest, BackSplitResponse.class);
    }


    /**
     * 厦门国际银行-支付查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<PaymentQueryResponse> xibPaymentQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, PaymentQueryResponse.class);
    }

    /**
     * 厦门国际银行-退款查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<RefundQueryResponse> xibRefundQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, RefundQueryResponse.class);
    }

    /**
     * 厦门国际银行-确认收货查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<ReceiveQueryResponse> xibReceiveQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, ReceiveQueryResponse.class);
    }

    /**
     * 厦门国际银行-结算查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<WithdrawQueryResponse> xibWithdrawQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, WithdrawQueryResponse.class);
    }

    /**
     * 厦门国际银行-充值查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<DepositQueryResponse> xibDepositQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, DepositQueryResponse.class);
    }

    /**
     * 厦门国际银行-转账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<TransferQueryResponse> xibTransferQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, TransferQueryResponse.class);
    }

    /**
     * 厦门国际银行-异步分账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<CompleteQueryResponse> xibCompleteQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, CompleteQueryResponse.class);
    }

    /**
     * 厦门国际银行-分账归还查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<BackSplitResponse> xibBackSplitQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, BackSplitResponse.class);
    }

    /**
     * 厦门国际银行-XIB分账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<XibSplitResponse> xibSplitQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.XIB)
                .build();
        return post(reqApiRequest, XibSplitResponse.class);
    }




    /**
     * 易宝-支付查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<PaymentQueryResponse> ybPaymentQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, PaymentQueryResponse.class);
    }

    /**
     * 易宝-退款查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<RefundQueryResponse> ybRefundQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, RefundQueryResponse.class);
    }

    /**
     * 易宝-确认收货查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<ReceiveQueryResponse> ybReceiveQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, ReceiveQueryResponse.class);
    }

    /**
     * 易宝-结算查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<WithdrawQueryResponse> ybWithdrawQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, WithdrawQueryResponse.class);
    }

    /**
     * 易宝-充值查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<DepositQueryResponse> ybDepositQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, DepositQueryResponse.class);
    }

    /**
     * 易宝-转账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<TransferQueryResponse> ybTransferQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, TransferQueryResponse.class);
    }

    /**
     * 易宝-异步分账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<CompleteQueryResponse> ybCompleteQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, CompleteQueryResponse.class);
    }

    /**
     * 易宝-分账归还查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<BackSplitResponse> ybBackSplitQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.YB)
                .build();
        return post(reqApiRequest, BackSplitResponse.class);
    }


    /**
     * 汇付-支付查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<PaymentQueryResponse> hfPaymentQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, PaymentQueryResponse.class);
    }

    /**
     * 汇付-退款查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<RefundQueryResponse> hfRefundQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, RefundQueryResponse.class);
    }

    /**
     * 汇付-确认收货查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<ReceiveQueryResponse> hfReceiveQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, ReceiveQueryResponse.class);
    }

    /**
     * 汇付-结算查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<WithdrawQueryResponse> hfWithdrawQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, WithdrawQueryResponse.class);
    }

    /**
     * 汇付-充值查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<DepositQueryResponse> hfDepositQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, DepositQueryResponse.class);
    }

    /**
     * 汇付-转账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<TransferQueryResponse> hfTransferQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, TransferQueryResponse.class);
    }

    /**
     * 汇付-异步分账查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<CompleteQueryResponse> hfCompleteQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, CompleteQueryResponse.class);
    }

    /**
     * 汇付-分账归还查询
     *
     * @param request
     * @return
     */
    private static BaseResponse<BackSplitResponse> hfBackSplitQuery(QueryOrderRequest request) {
        ApiRequest reqApiRequest = new ApiRequest.ApiRequestBuild()
                .merOrderId(request.getMerOrderId())
                .merchantNo(request.getMerchantNo())
                .params(request)
                .serverCode(ServerCodeEnum.QUERY_ORDER)
                .channelType(ChannelTypeEnum.HF)
                .build();
        return post(reqApiRequest, BackSplitResponse.class);
    }
}
